<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let s1 = new Set([1, 2, 3, 4, 2, 1])
        console.log(s1) //{1,2,3,4}
        console.log([...s1]) //[1,2,3,4]
        console.log(Array.from(s1)) //[1,2,3,4]

        let s2 = new Set()
        s2.add(11)
        s2.add(22)
        s2.add(22)
        s2.add(33)
        console.log(s2) //{1,2,3}

        //set实例属性和方法
        //size：返回Set实例的成员总数
        console.log(s1.size) //4
        //add()：添加成员
        s1.add(5).add(6)
        //has()：判断是否存在指定成员
        console.log(s1.has(5)) //true
        //delete()：删除指定成员
        s1.delete(5)
        console.log(s1.has(5)) //false
        //clear()：清除set中所有成员
        s1.clear()

        //keys()：返回键名的遍历器
        for (let i of s2.keys()) {
            console.log(i)
        }
        //values()：返回键值的遍历器
        for (let i of s2.values()) {
            console.log(i)
        }
        //entries()：返回键值对的遍历器
        for (let i of s2.entries()) {
            console.log(i)
        }
        //使用解构赋值
        let arr = ["aa", "bb", "cc"]
        for (let [index, item] of arr.entries()) {
            console.log(index, item)
        }

        //forEach()：使用回调函数遍历每个成员
        s2.forEach((item, index) => {
            console.log(item, index)
        })

        //实现对复杂数组进行去重
        let arr2 = [1, 2, 2, "twj", "twj", [1, 2], [3, 4], [1, 2], { name: "twj" }
            , { age: 100 }, { name: "twj" }, undefined, undefined, NaN, NaN]
        function uni(arr2) {
            let res = new Set()
            return arr2.filter((item) => {
                //判断有没有重复的，没有就返回true
                let id = JSON.stringify(item)
                if (res.has(id)) {
                    return false
                } else {
                    res.add(id)
                    return true
                }
            })
        }
        console.log(uni(arr2))
    </script>
</body>

</html>